﻿package mortal.game.view.mainUI.smallMap.scene
{
    import Message.Public.*;
    import __AS3__.vec.*;
    import baseEngine.modifiers.*;
    import com.gengine.core.frame.*;
    import com.gengine.utils.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.component.gLinkText.*;
    import mortal.component.gconst.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.control.subControl.*;
    import mortal.game.model.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.ai.ais.*;
    import mortal.game.scene3D.ai.base.*;
    import mortal.game.scene3D.ai.singleAIs.autofight.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.data.*;
    import mortal.game.scene3D.map3D.mapToMapPath.*;
    import mortal.game.scene3D.map3D.sceneInfo.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.utils.*;
    import mortal.game.view.task.data.*;

    public class ScenePathShower extends Object
    {
        public var _arr:Array;
        private var _effects:Array;
        private var _timer:FrameTimer;
        private var _lastLinkDta:GLinkTextData;
        private var _sceneInfo:SceneInfo;

        public function ScenePathShower()
        {
            this._effects = [];
            return;
        }// end function

        public function startFrame() : void
        {
            if (this._timer == null)
            {
                this._timer = new FrameTimer(60);
            }
            this._timer.addListener(TimerType.ENTERFRAME, this.onEnterFrameHandler);
            this._timer.start();
            return;
        }// end function

        private function onEnterFrameHandler(param1 = null) : void
        {
            var _loc_12:TaskInfo = null;
            var _loc_13:int = 0;
            var _loc_14:int = 0;
            var _loc_15:IAICommand = null;
            var _loc_16:Dictionary = null;
            var _loc_17:MonsterPlayer = null;
            var _loc_18:AstarTurnPoint = null;
            var _loc_19:Array = null;
            var _loc_20:int = 0;
            var _loc_21:IEntity = null;
            var _loc_22:SceneInfo = null;
            var _loc_23:NPCInfo = null;
            var _loc_24:Vector.<SPassPoint> = null;
            var _loc_25:SceneInfo = null;
            var _loc_26:SPassPoint = null;
            var _loc_27:SPassTo = null;
            var _loc_28:Point = null;
            var _loc_2:* = Cache.instance.task.getNextAutoPathTask();
            if (CopyUtil.isInGuideDefenseCopy)
            {
                _loc_12 = Cache.instance.task.getTaskByCode(GameConst.GuideDefenceCopyTaskCode, true);
                if (_loc_12 != null)
                {
                    _loc_2 = _loc_12;
                    if (_loc_2.curStep <= 1 || !_loc_2.isDoing())
                    {
                        this.clear();
                        return;
                    }
                }
            }
            if (_loc_2 == null)
            {
                this.clear();
                return;
            }
            var _loc_3:* = Cache.instance.task.getCurLinkTextDatas(_loc_2);
            if (_loc_3 == null || _loc_3[0] == null)
            {
                this.clear();
                return;
            }
            var _loc_4:* = _loc_3[0];
            if (this._lastLinkDta != null && _loc_4.equal(this._lastLinkDta))
            {
                if (_loc_4.type == GLinkTextData.boss)
                {
                    _loc_13 = GLinkTextData(_loc_3[0]).value1;
                    _loc_14 = -1;
                    if (_loc_3[1] != null)
                    {
                        _loc_14 = GLinkTextData(_loc_3[1]).value1;
                    }
                    _loc_15 = AIManager.curAI;
                    if (_loc_15 is AutoFightAI)
                    {
                        _loc_16 = AutoFightBossSelectAI.instance.onlyIds;
                        if (_loc_16 != null && _loc_16[_loc_13] == true)
                        {
                            this.clear();
                            return;
                        }
                    }
                    else if (_loc_15 is FollowFightAI)
                    {
                        _loc_17 = _loc_15.data.target as MonsterPlayer;
                        if (_loc_17 != null && (_loc_17.bossInfo.code == _loc_13 || _loc_17.bossInfo.code == _loc_14))
                        {
                            this.clear();
                            return;
                        }
                    }
                }
                if (this._arr != null && this._arr.length > 0)
                {
                    _loc_18 = this._arr[0];
                    if (_loc_18 != null && GeomUtil.calcDistance(RolePlayer.instance.x2d, RolePlayer.instance.y2d, _loc_18._x, _loc_18._y) <= 400)
                    {
                        return;
                    }
                }
            }
            var _loc_5:* = _loc_4.mapId;
            var _loc_6:* = _loc_4.x;
            var _loc_7:* = _loc_4.y;
            if (Cache.instance.role.roleInfo.level > 10 && CopyUtil.isInGuideDefenseCopy)
            {
                AutoFightBossSelectAI.instance.updateMyPoint();
                _loc_19 = ThingUtil.entityUtil.entitysMap.getEntityByRangle(null, EEntityType._EEntityTypeBoss, false);
                _loc_19.sort(AutoFightBossSelectAI.instance.sortByDistance);
                _loc_20 = 0;
                while (_loc_20 < _loc_19.length)
                {
                    
                    _loc_21 = _loc_19[_loc_20];
                    if (_loc_21 != null && AutoFightBossSelectAI.instance.isAttackable(_loc_21))
                    {
                        _loc_6 = _loc_21.x2d;
                        _loc_7 = _loc_21.y2d;
                        break;
                    }
                    _loc_20++;
                }
            }
            var _loc_8:* = GeomUtil.calcDistance(_loc_6, _loc_7, RolePlayer.instance.x2d, RolePlayer.instance.y2d);
            if (!CopyUtil.isInGuideDefenseCopy)
            {
                if (_loc_8 > 2000)
                {
                    this.clear();
                    return;
                }
            }
            if (_loc_4.type == GLinkTextData.boss || _loc_4.type == GLinkTextData.Point)
            {
            }
            else if (_loc_4.type == GLinkTextData.npc)
            {
                if (_loc_4.value2 != 0)
                {
                    this.clear();
                    return;
                }
                _loc_22 = SceneConfig.instance.getSceneInfoByNpcId(_loc_4.value1);
                if (_loc_22 != null)
                {
                    _loc_23 = _loc_22.getNpcInfo(_loc_4.value1);
                }
                if (_loc_23 == null)
                {
                    this.clear();
                    return;
                }
                _loc_5 = _loc_22.sMapDefine.mapId;
                _loc_6 = _loc_23.snpc.point.x;
                _loc_7 = _loc_23.snpc.point.y;
            }
            else
            {
                this.clear();
                return;
            }
            var _loc_9:* = Game.mapInfo.mapId;
            if (_loc_5 != _loc_9)
            {
                _loc_24 = MapPathSearcher.findMapPath(_loc_9, _loc_5);
                if (_loc_24 == null || _loc_24.length == 0)
                {
                    _loc_25 = SceneConfig.instance.getSceneInfo(_loc_9);
                    if (_loc_25.passInfos.length == 1)
                    {
                        _loc_26 = _loc_25.passInfos[0] as SPassPoint;
                        _loc_27 = _loc_26.passTo[0] as SPassTo;
                        if (_loc_27 == null || _loc_27.toPoint == null)
                        {
                            _loc_28 = new Point();
                            _loc_6 = _loc_26.point.x;
                            _loc_7 = _loc_26.point.y;
                        }
                    }
                }
                else
                {
                    _loc_26 = _loc_24[0];
                    _loc_27 = _loc_26.passTo[0] as SPassTo;
                    _loc_6 = _loc_26.point.x;
                    _loc_7 = _loc_26.point.y;
                }
            }
            this._lastLinkDta = _loc_4;
            var _loc_10:* = getTimer();
            var _loc_11:* = Scene3DClickProcessor.findPathImpl(RolePlayer.instance.x2d, RolePlayer.instance.y2d, _loc_6, _loc_7, true);
            this.updatePath(_loc_11[0]);
            return;
        }// end function

        public function stopFrame() : void
        {
            if (this._timer != null)
            {
                this._timer.stop();
            }
            return;
        }// end function

        public function updatePath(param1:Array) : void
        {
            this._arr = [];
            if (param1 == null || param1.length <= 1)
            {
                return;
            }
            this._arr.push(param1[1]);
            var _loc_2:int = 3;
            while (_loc_2 < param1.length - 2)
            {
                
                this._arr.push(param1[_loc_2]);
                _loc_2 = _loc_2 + 3;
            }
            this._arr.push(param1[(param1.length - 1)]);
            this.updatePathShow();
            return;
        }// end function

        public function updatePathShow() : void
        {
            if (this._arr == null || this._arr.length == 0)
            {
                this.clear();
                return;
            }
            var _loc_1:* = RolePlayer.instance.x2d;
            var _loc_2:* = RolePlayer.instance.y2d;
            var _loc_3:* = this._arr[(this._arr.length - 1)];
            if (GeomUtil.calcDistance(_loc_3._x, _loc_3._y, _loc_1, _loc_2) <= 300)
            {
                this.clear();
                return;
            }
            var _loc_4:* = Math.min(this._arr.length, 5);
            var _loc_5:int = -1;
            var _loc_6:int = 0;
            while (_loc_6 < _loc_4)
            {
                
                _loc_3 = this._arr[_loc_6];
                if (GeomUtil.calcDistance(_loc_3._x, _loc_3._y, _loc_1, _loc_2) <= 75)
                {
                    _loc_5 = _loc_6;
                    break;
                }
                _loc_6++;
            }
            if (_loc_5 >= 0)
            {
                this._arr.splice(0, (_loc_5 + 1));
            }
            this.updateShow();
            return;
        }// end function

        private function updateShow() : void
        {
            var _loc_6:AstarTurnPoint = null;
            var _loc_7:EffectPlayer = null;
            var _loc_8:Number = NaN;
            var _loc_1:* = RolePlayer.instance.x2d;
            var _loc_2:* = RolePlayer.instance.y2d;
            this._sceneInfo = Game.sceneInfo;
            if (this._sceneInfo == null)
            {
                return;
            }
            var _loc_3:int = 0;
            while (_loc_3 < this._arr.length)
            {
                
                _loc_6 = this._arr[_loc_3];
                _loc_7 = this.getEffect(_loc_3);
                _loc_7.x2d = _loc_6._x;
                _loc_7.y2d = _loc_6._y;
                _loc_8 = MathUitl.getAngleByXY(_loc_6._x, _loc_6._y, _loc_1, _loc_2);
                _loc_7.rotationY = _loc_8 - 90;
                _loc_7.updateMaskable(true);
                _loc_1 = _loc_6._x;
                _loc_2 = _loc_6._y;
                if (!SceneRange.display.contains(_loc_6._x, _loc_6._y))
                {
                    break;
                }
                _loc_3++;
            }
            var _loc_4:* = this._effects.length;
            var _loc_5:* = _loc_3;
            while (_loc_3 < _loc_4)
            {
                
                _loc_7 = this._effects[_loc_3];
                if (_loc_7)
                {
                    _loc_7.stop();
                    _loc_7.dispose(true);
                }
                this._effects[_loc_3] = null;
                _loc_3++;
            }
            return;
        }// end function

        private function isInXLightRect(param1:AstarTurnPoint) : Boolean
        {
            if (this._sceneInfo.isXLightGrid(param1._x, param1._y))
            {
                return true;
            }
            var _loc_2:* = this._sceneInfo.shadowAreaInfos;
            if (_loc_2.length < 1)
            {
                return false;
            }
            return false;
        }// end function

        private function getEffect(param1:int) : EffectPlayer
        {
            var _loc_2:* = this._effects[param1];
            if (_loc_2 == null)
            {
                _loc_2 = EffectPlayerPool.instance.getEffectPlayer(GlobalStaticResUrl.ScenePathArrow, Game.scene, null, false);
                this._effects[param1] = _loc_2;
                _loc_2.play(PlayMode.ANIMATION_LOOP_MODE);
            }
            return _loc_2;
        }// end function

        public function clear() : void
        {
            var _loc_3:EffectPlayer = null;
            if (this._arr != null && this._arr.length > 0)
            {
                this._arr = [];
            }
            if (this._effects.length == 0)
            {
                return;
            }
            var _loc_1:* = this._effects.length;
            var _loc_2:int = 0;
            while (_loc_2 < _loc_1)
            {
                
                _loc_3 = this._effects[_loc_2];
                if (_loc_3)
                {
                    _loc_3.stop();
                    _loc_3.dispose(true);
                    this._effects[_loc_2] = null;
                }
                _loc_2++;
            }
            this._effects.length = 0;
            return;
        }// end function

    }
}
